package com.mp.approvalreq.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.mp.approvalreq.dao.OperateTemplateDao;
import com.mp.approvalreq.entity.dto.*;
import com.mp.approvalreq.entity.message.RequestTimeMsg;
import com.plusmoney.util.SQLLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.List;

public class OperateTemplateDaoImpl implements OperateTemplateDao {

	private static final Logger logger = LoggerFactory.getLogger(OperateTemplateDaoImpl.class);

	private Connection conn;

	public OperateTemplateDaoImpl(Connection conn) {
		this.conn = conn;
	}


	@Override
	public void changeGeneralRequestFlow(GeneralRequestDTO generalRequestDTO, Integer companyId, String newFlows) throws SQLException {

		String sql = " update pt_mp_general_request_flow " +
				" set Prequest_flow = ? where Pcompany_id = ? and Pproject_id = ? ";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setString(1, newFlows);
			ps.setInt(2, companyId);
			ps.setInt(3, generalRequestDTO.getProject());

			logger.info(SQLLogger.logSQL(sql, newFlows, companyId, generalRequestDTO.getProject()));

			ps.executeUpdate();
		}
	}


	@Override
	public void changeGeneralConditionRequestFlow(List<GeneralConditionFlowDTO> newFlows, Integer companyId) throws SQLException {

		String sql = " update approval.pt_mp_general_request_condition_flow_desc " +
				" set Frequest_flow = ? where Fid = ? and Fgeneral_flow_id = ? and Fcompany_id = ? ";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			for (GeneralConditionFlowDTO generalConditionFlowDTO : newFlows) {
				ps.setString(1, generalConditionFlowDTO.getRequestFlow());
				ps.setInt(2, generalConditionFlowDTO.getId());
				ps.setInt(3, generalConditionFlowDTO.getGeneralFlowId());
				ps.setInt(4, generalConditionFlowDTO.getCompanyId());

				logger.info(SQLLogger.logSQL(sql, generalConditionFlowDTO.getRequestFlow(), generalConditionFlowDTO.getId(),
						generalConditionFlowDTO.getGeneralFlowId(), generalConditionFlowDTO.getCompanyId()));

				ps.addBatch();
			}

			ps.executeBatch();
		}
	}

	@Override
	public void insertDaysOffTimes(RequestTimeMsg requestTimeMsg) throws SQLException {
		String sql = " insert into approval.request_time set request_id = ?, " +
				" days_off_time = ?, crt_time = now(), contact_id = ?, company_id = ?, company_info_id = ?, date = ? ";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setInt(1, requestTimeMsg.getRequestId());
			ps.setString(2, requestTimeMsg.getDaysOffTimes());
			ps.setInt(3, requestTimeMsg.getContactId());
			ps.setInt(4, requestTimeMsg.getCompanyId());
			ps.setInt(5, requestTimeMsg.getCompanyInfoId());
			ps.setString(6, requestTimeMsg.getDate());
			logger.info(SQLLogger.logSQL(sql, requestTimeMsg.getRequestId(), requestTimeMsg.getDaysOffTimes(),
					requestTimeMsg.getContactId(), requestTimeMsg.getCompanyId(), requestTimeMsg.getCompanyInfoId(), requestTimeMsg.getDate()));

			ps.executeUpdate();
		}
	}


	@Override
	public void updateDaysOffTimes(RequestTimeMsg requestTimeMsg) throws SQLException {
		String sql = " update approval.request_time set days_off_time = ?, contact_id = ?, company_id = ?, company_info_id = ? " +
				" where request_id = ? and date = ? limit 1";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setString(1, requestTimeMsg.getDaysOffTimes());
			ps.setInt(2, requestTimeMsg.getContactId());
			ps.setInt(3, requestTimeMsg.getCompanyId());
			ps.setInt(4, requestTimeMsg.getCompanyInfoId());
			ps.setInt(5, requestTimeMsg.getRequestId());

			ps.setString(6,requestTimeMsg.getDate());
			logger.info(SQLLogger.logSQL(sql, requestTimeMsg.getDaysOffTimes(), requestTimeMsg.getContactId(), requestTimeMsg.getCompanyId(),
					requestTimeMsg.getCompanyInfoId(), requestTimeMsg.getRequestId()));

			ps.executeUpdate();
		}
	}

	@Override
	public void deleteComponentGroup(Integer companyId, Integer componentGroupId) throws SQLException {
		String sql = " update request_component_group set state = 1 where id = ? and company_id = ? limit 1";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setInt(1, componentGroupId);
			ps.setInt(2, companyId);
			logger.info(SQLLogger.logSQL(sql, componentGroupId, companyId));

			ps.executeUpdate();
		}
	}


	@Override
	public int insertComponentGroup(ComponentGroupDTO componentGroupDTO, Integer companyId) throws SQLException {
		String sql = " insert into request_component_group set company_id = ?, size = ?, name = ?, sub_component = ?, is_add_component_group = ?, is_common_group = ?, crt_time = now() ";

		try (PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
			ps.setInt(1, companyId);
			ps.setInt(2, componentGroupDTO.getSize());
			ps.setString(3, componentGroupDTO.getName());
			ps.setString(4, JSONArray.toJSONString(componentGroupDTO.getComponents()));
			ps.setInt(5, componentGroupDTO.getIsAddComponentGroup());
			ps.setInt(6, componentGroupDTO.getIsCommonGroup());
			logger.info(SQLLogger.logSQL(sql, companyId, componentGroupDTO.getSize(), componentGroupDTO.getName(),
					JSONArray.toJSONString(componentGroupDTO.getComponents()), componentGroupDTO.getIsAddComponentGroup(), componentGroupDTO.getIsCommonGroup()));

			ps.executeUpdate();
			try (ResultSet rs = ps.getGeneratedKeys()) {
				if (rs.next()) {
					return rs.getInt(1);
				}
			}
		}
		return 0;
	}
}
